package com.itheima.mybatisplusdemo;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.mybatisplusdemo.domain.User;
import com.itheima.mybatisplusdemo.mapper.UserDao;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
@Slf4j
public class MybatisPlusQueryInfo {

    @Autowired
    private UserDao userDao;

    @Test
    public void testLoginQuery() {
        //模拟前端账号密码
        String username = "admin";
        String password = "123456";

        //需要判空,都不为空才去查询数据库
        if (password == null || password == "" || username == null || username == "") {
            return;
        }

        //查询一条数据
        User user = userDao.selectOne(Wrappers.<User>lambdaQuery()
                .eq(User::getName, username)
                .eq(User::getPassword, password));

    }

    /**
     * 查询年龄
     */

    @Test
    public void testAge() {
        //模拟年龄
        Integer minAge = 10;
        Integer maxAge = 20;

        List<User> users = userDao.selectList(Wrappers.<User>lambdaQuery()
                .gt(minAge != null, User::getAge, minAge)
                .lt(maxAge != null, User::getAge, maxAge));
        for (User user : users) {
            log.info("用户信息: " + user.toString());
        }

        //方案2: between and
        List<User> users1 = userDao.selectList(Wrappers.<User>lambdaQuery()
                .between(User::getAge, minAge, maxAge));
        for (User user : users1) {
            log.info(user.toString());
        }
    }

    /**
     * in
     */
    @Test
    public void testQueryIn() {
        List<User> users = userDao.selectList(Wrappers.<User>lambdaQuery()
                .in(User::getId, 1L, 2L, 5L)
                .orderByAsc(User::getAge));//升序
        for (User user : users) {
            log.info(user.toString());
        }
    }

    /**
     * order by排序
     */





}
